/**
 * @author Derek
 * @create 2016/12/28
 */
$(function() {
    initLeftTree();
});

//初始化授权树
function initLeftTree(){
    var zNodes = [];

    var setting = {
        view: {
            enable: true,
            showIcon:false
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };

    $.ajax({
        url : '/perm/tree',
        type : "post",
        dataType : 'json',
        async : false,
        success : function(data) {
            console.log( data.data);
            if(data && data.code == Const.SUCCESS ){
                zNodes = data.data;
            }
        }
    });
    $.fn.zTree.init($("#treeview-checkable"), setting, zNodes);
}

/**
 * 选择节点
 * @param e
 * @param treeId
 * @param treeNode
 */
function onClick(e, treeId, treeNode) {
    var zTree = $.fn.zTree.getZTreeObj("treeSelect");
    var nodes = zTree.getSelectedNodes();
    $('#parent').val(nodes[0].name);
    $('#parentId').val(nodes[0].id);
}

function showMenu() {
    initTree();
    var obj = $("#parent");
    var offset = $("#parent").offset();
    $("#menuContent").css({left:offset.left + "px", top:offset.top + obj.outerHeight() + "px"}).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}

function hideMenu() {
    $("#menuContent").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);
}
function onBodyDown(event) {
    if (!(event.target.id == "menuBtn"
        || event.target.id == "menuContent"
        || $(event.target).parents("#menuContent").length>0)) {
        hideMenu();
    }
}

/**
 * 下拉框的tree
 */
function initTree(){
    var zNodes2 = [];

    var setting = {
        view: {
            dblClickExpand: false,
            showIcon:false
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick: onClick
        }
    };
    $.ajax({
        url : '/perm/selectTree',
        type : "post",
        dataType : 'json',
        async : false,
        success : function(data) {
            console.log( data.data);
            if(data && data.code == Const.SUCCESS ){
                zNodes2 = data.data;
            }
        }
    });
    $.fn.zTree.init($("#treeSelect"), setting, zNodes2);
}

//保存授权
function save(){
    var parentId = $('#parentId').val();
    var name = $('#name').val();
    var code = $('#code').val().trim();
    var param = {
        parentId : parentId,
        name : name,
        code : code
    }
    warnConfirm("是否确认保存？", function(result){
        if(result) {
            ajaxPost("/perm/save", param, function(data){
                //console.log(data);
                if (data && data.code == Const.SUCCESS) {
                    success("保存成功！");
                    $('#parentId').val('');
                    $('#parent').val('');
                    $('#name').val('');
                    $('#code').val('');
                    initLeftTree();
                }else {
                    error("警告",data.data);
                }
            });
        }
    });
}

